/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fiap.util;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 *
 * @author ronildo
 */
public class ProjectLoggerFactory {

    private static final String PATH = "/usr/sistematicos.log";
    private static final String LEVEL = "8";
    private static Logger logger;

    private static Level getLevel() {
        int level = Integer.parseInt(LEVEL);

        switch (level) {
            case 1:
                return Level.SEVERE;
            case 2:
                return Level.WARNING;
            case 3:
                return Level.INFO;
            case 4:
                return Level.CONFIG;
            case 5:
                return Level.FINE;
            case 6:
                return Level.FINER;
            case 7:
                return Level.FINEST;
            case 8:
                return Level.ALL;
            case 9:
                return Level.OFF;
            default:
                return Level.OFF;
        }
    }

    private static Logger getLoggerFactory() {
        logger = Logger.getLogger(ProjectLoggerFactory.class.getName());
        try {
            FileHandler fh = new FileHandler(PATH, true);
            fh.setFormatter(new SimpleFormatter());

            logger.addHandler(fh);
            logger.setLevel(getLevel());
        } catch (IOException ex) {
            logger.log(Level.WARNING, "Nao foi possivel definir um arquivo de log para o sistema", ex);
        }
        return logger;
    }

    public static Logger getLogger() {
        if (logger == null) {
            logger = getLoggerFactory();
            logger.info("Instancia de log criado com sucesso");
            return logger;
        } else {
            return logger;
        }
    }
}
